/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SearchResults.java
 *
 * Created on Mar 28, 2011, 3:45:36 PM
 */

package POS_Source;

import Cart.Cart;
import DTO.Item;
import POS_Controller.POSController;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.ListIterator;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.table.TableColumn;

/**
 * this class renders search results view 
 * @author EDAYPE
 * @see POS_Source
 */
public class POS_SearchResults extends javax.swing.JPanel {

    private POSController controller;
    private ArrayList<Item> searchResults;
    private Cart cart;
   /**
    * constructor for this class
    * @param control
    */
    public POS_SearchResults(POSController control) {
        initComponents();
        controller = control;
        cart = controller.getCart();
        searchResults = cart.getSearchResults();
        customInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ButtonPanel = new javax.swing.JPanel();
        SearchTitle = new javax.swing.JLabel();
        DetailsButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        ButtonPanel.setBackground(new java.awt.Color(255, 255, 255));

        SearchTitle.setFont(new java.awt.Font("Copperplate Gothic Bold", 3, 48));
        SearchTitle.setText("Search Results");

        DetailsButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        DetailsButton1.setText("View Details");
        DetailsButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DetailsButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
        ButtonPanel.setLayout(ButtonPanelLayout);
        ButtonPanelLayout.setHorizontalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SearchTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 495, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 124, Short.MAX_VALUE)
                .addComponent(DetailsButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        ButtonPanelLayout.setVerticalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ButtonPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(DetailsButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SearchTitle))
                .addGap(8, 8, 8))
        );

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Image", "Name", "Make", "Model", "Price"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setRowHeight(70);
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Please select an item and click View Details");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(227, 227, 227)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 768, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 443, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
/**
 * this method handles detail button press event 
 * @param evt
 */
    private void DetailsButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DetailsButton1ActionPerformed
        Item selection = null;
        int row = jTable1.getSelectedRow();
        if( row >= 0){
            selection = cart.getSearchResults().get(row);
            controller.ItemDescriptionScreen(selection);
        }
        
    }//GEN-LAST:event_DetailsButton1ActionPerformed
/**
 * this method overrides default view rendering 
 */
    public final void customInit(){
        BufferedImage image = null;

        Object[][] obj = new Object[searchResults.size()][5];
        ListIterator<Item> iter = searchResults.listIterator();
        int count = 0;
        System.out.println(searchResults.size());
        if (searchResults.size()>0){
            while (iter.hasNext()){
                Item temp = iter.next();
                try {
                   // URL url = new URL("http://www.laboratoriosdelcentro.com/digistore/10126271.png");
                   String location = System.getProperty("user.dir");
                   location += "/src/POS_Source/images/" + temp.getImgURL();
                   File imageFile = new File(location);
                   image = ImageIO.read(imageFile);
                } catch (IOException e) {
                }
                BufferedImage resizedImage = resize(image,100,100);
                ImageIcon icon = new ImageIcon(resizedImage);

                obj[count][0] = icon;
                obj[count][1] = temp.getName();
                obj[count][2] = temp.getMake();
                obj[count][3] = temp.getModel();
                obj[count][4] = new DecimalFormat("##,###.00").format(temp.getRetailPrice());
                count++;
            }
        }

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            obj,
            new String [] {
                "Image", "Name", "Make", "Model", "Price"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
            public Class getColumnClass(int columnIndex)
            {
            return columnIndex == 0 ? ImageIcon.class : Object.class;
            }
        });

        TableColumn column = null;
        column = jTable1.getColumnModel().getColumn(0);
        column.setPreferredWidth(100); //third column is bigger
        column = jTable1.getColumnModel().getColumn(1);
        column.setPreferredWidth(325);
        column = jTable1.getColumnModel().getColumn(2);
        column.setPreferredWidth(75);
        column = jTable1.getColumnModel().getColumn(3);
        column.setPreferredWidth(100);
        column = jTable1.getColumnModel().getColumn(4);
        column.setPreferredWidth(100);

        jTable1.setRowHeight(100);
        jTable1.setColumnSelectionAllowed(false);
        jTable1.setRowSelectionAllowed(true);
    }
/**
 * this method resizes the image
 * @param image
 * @param width
 * @param height
 * @return
 */
    private BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage resizedImage = new BufferedImage(width, height,
        BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();
        return resizedImage;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ButtonPanel;
    private javax.swing.JButton DetailsButton1;
    private javax.swing.JLabel SearchTitle;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
